package com.example.areaofthesquare;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

public class GridViewAdapter extends BaseAdapter {
    private List<Point> mList;
    private LayoutInflater inflater;
    private Context mContext;

    public GridViewAdapter(Context context,List<Point> mList) {
        this.mContext = context;
        this.mList = mList;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null){
            convertView = inflater.from(mContext).inflate(R.layout.grid_view_item,null);

            holder = new ViewHolder();
            holder.textView = convertView.findViewById(R.id.tv_item);
            convertView.setTag(holder);
        } else{
            holder = (ViewHolder) convertView.getTag();
        }
        String x = String.valueOf(mList.get(position).getX());
        String y = String.valueOf(mList.get(position).getY());
        if(mList != null){
            String text = "[" + x + " , " + y + "]";
            holder.textView.setText(text);
        }
        return convertView;
    }

    class ViewHolder{
        TextView textView;
    }

}
